/**   
 * @Title: JsonUtil.java 
 * @Package android.framework.util 
 * @Description: TODO
 * @author jtf@163.com   
 * @date 2013-7-26 上午9:55:52 
 * @version V1.0   
 */
package android.framework.util;

import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
 * @ClassName: JsonUtil
 * @Description: TODO
 * @author jtf19@163.com
 * @date 2013-7-26 上午9:55:52
 * 
 */
public class JsonUtil {

	private static Gson gson = new Gson();

	public static Map<String, Object> json2Map(String json) throws Exception {
		if (TextUtils.isEmpty(json)) {
			throw new Exception("json is null");
		}
		Type type = new TypeToken<Map<String, Object>>() {
		}.getType();
		return gson.fromJson(json, type);
	}

	public static List<Map<String, Object>> json2List(String json)
			throws Exception {
		if (TextUtils.isEmpty(json)) {
			throw new Exception("json is null");
		}
		Type type = new TypeToken<List<Map<String, Object>>>() {
		}.getType();
		return gson.fromJson(json, type);
	}

	public static void main(String[] args) {
		String json = "[{\"name\":\"name0\",\"age\":0},{\"name\":\"name1\",\"age\":5},{\"name\":\"name2\",\"age\":10},{\"name\":\"name3\",\"age\":15},{\"name\":\"name4\",\"age\":20},{\"name\":\"name5\",\"age\":25},{\"name\":\"name6\",\"age\":30},{\"name\":\"name7\",\"age\":35},{\"name\":\"name8\",\"age\":40},{\"name\":\"name9\",\"age\":45}]";
		Gson gson = new Gson();
		Type type = new TypeToken<List<Map<String, Object>>>() {
		}.getType();
		List<Map<String, String>> list = gson.fromJson(json, type);
		System.out.println(list);
	}
}
